package IO.序列化;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.jupiter.api.Assertions;

import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;

class fileToPojoList {
	
	static ObjectMapper objectMapper = new ObjectMapper();
	
	public static void main(String[] args) throws IOException {
	    File file = new File("f:\\PersonList.json");
	    //Person person=new Person();
	    List<Person> personList = objectMapper.readValue(file, new TypeReference<List<Person>>() {});
	    for (Person person : personList) {
	        System.out.println(person);
	    }
	    Assertions.assertEquals(personList.size(), 2);
	    Assertions.assertEquals(personList.get(0).getName(), "aLang");
	    Assertions.assertEquals(personList.get(1).getName(), "darcy");
	    
	    //把 JSON 文本转成 Map 对象。
	    //•objectMapper.readValue(String jsonObject，new TypeReference <Map <String，Object >>(){}) 从JSON字符串中读取映射为map
	    //String jsonObject ="{\"brand\":\"ford\",\"doors\":5}";
	    //Map <String，Object> jsonMap = objectMapper.readValue(jsonObject,new TypeReference <Map <String,Object>>(){});
	    String expectedJson = "{\"name\":\"aLang\",\"age\":27,\"skillList\":[\"java\",\"c++\"]}";
	    Map<String, Object> employeeMap = objectMapper.readValue(expectedJson, new TypeReference<Map<String, Object>>() {});
	    System.out.println(employeeMap.getClass());
	    for (Entry<String, Object> entry : employeeMap.entrySet()) {
	        System.out.println(entry.getKey() + ":" + entry.getValue());
	    }
	    Assertions.assertEquals(employeeMap.get("name"), "aLang");
	    
	    List<Integer> list = new ArrayList<>();
       list.add(1);
       list.add(9);
       list.add(4);
       list.add(8);
       //ObjectMapper jsonObject = new ObjectMapper();
       JSONObject jsonObj = new JSONObject();
       jsonObj.put("a", list);
       System.out.println(jsonObj);//{"a":[1,9,4,8]}

       List<String> list2 =  (List<String>) jsonObj.get("a");//Object("a", new TypeReference<List<Integer>>(){});
       System.out.println(list2);
       
       //首先从 JSON 格式的字符串中构造一个 JSON 对象，之后依次读取字符串、整数、布尔值以及数组，最后分别打印
       JSONObject object = JSONObject
      	      .parseObject("{\"boolean\":true,\"string\":\"string\",\"list\":[1,2,3],\"int\":2}");
      //string
      String s = object.getString("string");
      System.out.println(s);
      //int
      int i = object.getIntValue("int");
      System.out.println(i);
      //boolean
      boolean b = object.getBooleanValue("boolean");
      System.out.println(b);
      //list
      List<Integer> integers = JSON.parseArray(object.getJSONArray("list").toJSONString(),Integer.class);
      integers.forEach(System.out::println);
      //null
      System.out.println(object.getString("null"));
	    
	}
}


/*
PersonList.json.
[
  {
    "name": "aLang",
    "age": 27,
    "skillList": [
      "java",
      "c++"
    ]
  },
  {
    "name": "darcy",
    "age": 26,
    "skillList": [
      "go",
      "rust"
    ]
  }
]
*/
